require( '../../../../config' );
var accesstokenDao = require( '../../../../../app/components/backend/db').accesstoken,
    assert = require('assert'),
    helper = require('../../../../helper'),
    testData = {
      access_token: 'BYEsg8wnKAM0d_7uxOjq7E7LgTeCPBw-FQa_-NVUviAMjMSKFD-85Z7rfrJa9vLX4-g3IpDJeKh6EpAp231gofjGokYc23nC9oQ4FX_k2Cs',
      create_time: 1449009999000
    },
    testData2 = {
      access_token: 'AbCDefGHijKLM_7uxOjq7E7LgTeCPBw-FQa_-NVUviAMjMSKFD-85Z7rfrJa9vLX4-g3IpDJeKh6EpAp231gofjGokYc23nC9oQ4FX_k2Cs',
      create_time: 1449099999999
    };

describe('app/components/backend/db/daos/accesstoken', function() {
  describe('#upsert()', function () {
    it('[upsert.Nor.01].创建/更新一个accesstoken', function () {

      // 插入数据1
      var upsertPromise = accesstokenDao.upsert({}, testData);

      // 验证mongoose返回的是基于bluebird的Promise
      assert.ok(helper.isBluebirdPromise(upsertPromise));

      // 验证mongoose返回的是基于bluebird的Promise
      var findOnePromise = accesstokenDao.findOne();
      assert.ok(helper.isBluebirdPromise(findOnePromise));

      return findOnePromise.then(function (data) { // 验证
          assert.equal(testData.access_token, data.access_token);
          assert.equal(testData.create_time, data.create_time);
        }).then(function () { // 插入数据2
          return accesstokenDao.upsert({}, testData2);
        }).then(accesstokenDao.findOne).then(function (data) { // 验证
          assert.equal(testData2.access_token, data.access_token);
          assert.equal(testData2.create_time, data.create_time);
        });
    });
  });
});